<?php

namespace App\Mobile\Controllers;

use DB;
use Auth;
use Validator;
use Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Agent;

class PersonController extends Controller
{
	/**
	 * [index description]
	 * @return [type] [description]
	 */
	public function index(Request $request, Agent $agent)
	{
		return view('mobile.person.index', compact('agent'));
	}

	/**
	 * [home description]
	 * @return [type] [description]
	 */
	public function home(Request $request, Agent $agent)
	{
		$parent_agent = [];
		if ($agent->pid != 0) {
			$parent_agent = Agent::where(['id' => $agent->pid])->first();
		}
		return view('mobile.agent.index', compact('agent', 'parent_agent'));
	}

	/**
	 * [person_add 完善用户信息]
	 * @return [type] [description]
	 */
	public function personAdd(Request $request, Agent $agent)
	{
        $validator = Validator::make($request->input(), [
            'realname' => 'required|max:5',
            'id_card' => 'required|max:18',
            'qq' => 'required|max:20',
            'address' => 'required|max:100'
        ],[
			'realname.required' => '真实姓名必填',
			'realname.max' => '真实姓名不超过5个字',
			'id_card.required' => '身份证号必填',
			'id_card.max' => '身份证不超过18位',
			'qq.required' => 'qq号码必填',
			'qq.max' => 'qq号码不超过20位',
			'address.required' => '常用地址必填',
			'address.max' => '地址不超过100位'
        ]);
        if ($validator->passes()) {
        	$agent->realname = $request->input('realname');
        	$agent->id_card = $request->input('id_card');
        	$agent->qq = $request->input('qq');
        	$agent->address = $request->input('address');
        	$agent->save();

	    	return redirect('/mobile/agent/person/'.$agent->id);
        } else {
        	return Redirect::back()->withInput()->withErrors($validator->errors());
        }
	}

	/**
	 * [address 用户地址]
	 * @return [type] [description]
	 */
	public function address(Agent $agent)
	{
		return view('mobile.person.address', compact('agent'));
	}

	/**
	 * [addressStore 保存地址]
	 * @return [type] [description]
	 */
	public function addressStore(Request $request, Agent $agent)
	{
        $validator = Validator::make($request->input(), [
            'address' => 'required|max:100'
        ],[
			'address.required' => '常用地址必填',
			'address.max' => '地址不超过100位'
        ]);
        if ($validator->passes()) {
        	$agent->address = $request->input('address');
        	$agent->save();

	    	return redirect('/mobile/agent/person/'.$agent->id);
        } else {
        	return Redirect::back()->withInput()->withErrors($validator->errors());
        }
	}

	
	
}	